package org.example.domain.strategy.model.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.example.types.common.Constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StrategyRuleEntity {
//      抽奖策略ID
    private Long strategyId;
//      奖品ID
    private Integer awardId;
//      抽奖规则类型【1-策略规则、2-奖品规则】
    private Integer ruleType;
//      抽奖规则比值
    private String ruleValue;
//      抽奖规则模型【rule_lock、rule_random】
    private String ruleModel;
//      抽奖规则描述
    private String ruleDesc;

    public Map<String, List<Integer>> getRuleValues(){
        if (!"rule_weight".equals(ruleModel)) return null;
        //1.通过空格划分
        String[] ruleValues = ruleValue.split(Constants.SPACE);
        Map<String, List<Integer>> resultMap = new HashMap<>();
        for (String ruleValue : ruleValues){
            if (ruleValue == null || ruleValue.isEmpty()){
                return resultMap;
            }
            String[] parts = ruleValue.split(Constants.COLON);
            if (parts.length !=2){
                throw new IllegalArgumentException("rule_weight rule_rule invalid input format" + ruleValue);
            }
            String[] valueStrings = parts[1].split(Constants.SPLIT);
            List<Integer> values = new ArrayList<>();
            for (String valueString : valueStrings){
                values.add(Integer.parseInt(valueString));
            }
            resultMap.put(ruleValue,values);
        }
        return resultMap;
    }
}
